//
//  MsgTableCtr.swift
//  sinaDemo
//
//  Created by admin on 2021/2/28.
//

import UIKit
import Lottie
import WidgetKit
class MsgTableCtr: XMBaseTableCtr {
    let linkctr:LinkTestCtr = LinkTestCtr()
    
    ///弹出视图遵循代理的自定义class
    lazy var PopPresentDelegate:PopTransitioning = PopTransitioning.init { [weak self](presened) in
        //self强引用了 PopPresentDelegate，PopPresentDelegate又强引用了闭包，闭包里面又有self，需要打破循环引用
        self?.titleView.isSelected = presened
    }
    lazy var jumpBtn:UIButton = UIButton(title: "点我进入菜单", font: 15, backColor: .orange).then({
        $0.frame = CGRect(x: kScreenWidth/2 - 100, y: kNavigationHeight + 20, width: 200, height: 50)
        $0.addTarget(self, action: #selector(self.jumpToMenu), for: .touchUpInside)
    })
    lazy var jumpLinkBtn = UIButton(title: "点我进入链表测试", font: 15, backColor: .orange).then({
        $0.frame = CGRect(x: kScreenWidth/2 - 100, y: jumpBtn.bottom + 10, width: 200, height: 50)
        $0.addTarget(self, action: #selector(self.jumpToLinkList), for: .touchUpInside)
    })
    lazy var jumpRunLoopBtn = UIButton(title: "点我进入RunLoop", font: 15, backColor: .orange).then {
        $0.frame = CGRect(x: jumpLinkBtn.left, y: jumpLinkBtn.bottom + 10, width: jumpLinkBtn.width, height: jumpLinkBtn.height)
        $0.addTarget(self, action: #selector(self.jumpToRunLoop), for: .touchUpInside)
    }
    lazy var jumpGCDBtn = UIButton(title: "点我进入GCD", font: 15, backColor: .orange).then {
        $0.frame = CGRect(x: jumpLinkBtn.left, y: jumpRunLoopBtn.bottom + 10, width: jumpLinkBtn.width, height: jumpLinkBtn.height)
        $0.addTarget(self, action: #selector(self.jumpToGCD), for: .touchUpInside)
    }
    lazy var changeIconBtn = UIButton(title: "点我换AppIcon", font: 15, backColor: .orange).then {
        $0.frame = CGRect(x: jumpLinkBtn.left, y: jumpGCDBtn.bottom + 10, width: jumpLinkBtn.width, height: jumpLinkBtn.height)
        $0.addTarget(self, action: #selector(self.jumpToChangeAppIcon), for: .touchUpInside)
    }
    lazy var reloadWidgetsBtn = UIButton(title: "点我刷新小组件", font: 15, backColor: .orange).then {
        $0.frame = CGRect(x: jumpLinkBtn.left, y: changeIconBtn.bottom + 10, width: jumpLinkBtn.width, height: jumpLinkBtn.height)
        $0.addTarget(self, action: #selector(self.reloadWidgets), for: .touchUpInside)
    }
    lazy var titleView:XMRightImgBtn = XMRightImgBtn()
    lazy var lottieView:AnimationView = AnimationView.init().then({
        $0.frame = CGRect(x: 100, y: jumpGCDBtn.bottom + 20, width: 200, height: 200)
        $0.center.x = kScreenWidth/2
        $0.isUserInteractionEnabled = true
        $0.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(self.touches)))
        $0.backgroundBehavior = .pauseAndRestore//暂停动画，并在应用程序移回到前台时重新启动它。动画完成时，将存储并调用完成块。
        $0.loopMode = .playOnce//loop会消耗cup
        self.view.addSubview($0)
    })
    override func viewDidLoad() {
        super.viewDidLoad()
        if self.isLogin {
            setUpNavgationBar()
        }else{
            visitorView.setupVistorUI(backImg: "tabbar_message_center", msg: "消息访客视图")
        }
        //!!!: 设置标记
        self.tabBarController?.children[1].tabBarItem.badgeValue = "99"
        
        setUpUI()
    }
    private func setUpUI(){
        let starAnimation = Animation.named("servishero_loading")
        lottieView.animation = starAnimation
        lottieView.play()
        view.addSubview(jumpBtn)
        view.addSubview(jumpLinkBtn)
        view.addSubview(jumpRunLoopBtn)
        view.addSubview(jumpGCDBtn)
        view.addSubview(changeIconBtn)
        view.addSubview(reloadWidgetsBtn)
    }
}
extension MsgTableCtr {
    private func setUpNavgationBar(){
        titleView.setTitle("wxm", for: .normal)
        titleView.addTarget(self, action: #selector(self.titleViewClick), for: .touchUpInside)
        navigationItem.titleView = titleView
    }
}
// MARK: - 点击事件

extension MsgTableCtr{
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("touches")
        let alert:UIAlertController = UIAlertController.init(title: nil, message: nil, preferredStyle: .alert)
        alert.addAction(UIAlertAction.init(title: "取消", style: .cancel, handler: { action in
            print(action.title ?? "")
        }))
        self.present(alert, animated: true) {
            print("alert")
        }
    }
    @objc func touches(){
        lottieView.play()
    }
    @objc func jumpToMenu(){
        navigationController?.pushViewController(thirdController.init(), animated: true)
    }
    @objc func jumpToLinkList(){
//        navigationController?.pushViewController(linkctr, animated: true)
        navigationController?.pushViewController(LinkTestCtr.init(), animated: true)
    }
    @objc func jumpToRunLoop(){
        navigationController?.pushViewController(MsgRunLoopCtr.init(), animated: true)
    }
    @objc func jumpToGCD(){
        navigationController?.pushViewController(MsgGCDCtr.init(), animated: true)
    }
    @objc func jumpToChangeAppIcon(){
        navigationController?.pushViewController(XMChangeIcon.init(), animated: true)
    }
    @objc func reloadWidgets(){
        if #available(iOS 14.0, *) {
            WidgetCenter.shared.reloadAllTimelines()
//            WidgetCenter.shared.reloadTimelines(ofKind: "XMWidgetClock")
        } else {
            // Fallback on earlier versions
        }
        
    }
    /// 弹出选择框ctr
    @objc func titleViewClick() {
        self.titleView.isSelected = !self.titleView.isSelected
        let poVc = PopViewCtr.init()
        //设置选择框的弹出代理为 PopPresentDelegate
        poVc.transitioningDelegate = PopPresentDelegate
        poVc.modalPresentationStyle = .custom
        self.present(poVc, animated: true, completion: nil)
    }
}
class thirdController:UIViewController,XMPagesDelegate{
    func addBtnClicked() {
        
    }
    func scrollDidScrolling(startPageTag: Int, endPageTag: Int, progress: CGFloat) {
        
    }
    func selectedPage(pageTag: Int) {
       print("selectedPage + \(pageTag)")
    }
    
    func scrollToPage(pageTag: Int) {
       print("scrollToPage + \(pageTag)")
    }
    
    var xmpageTool:XMPages? =  nil
    override func viewDidLoad() {
        view.backgroundColor = .white//设置背景色后，就能响应touchesBegan，否则就不行
//        xmpageTool = XMPages.init(titles: ["第一个","第二个","第三个"], viewControllers: [MsgSubRXTestCtr(),MsgSubRXTestCtr(),MsgSubRXTestCtr()], parentCtr: self)
        xmpageTool = XMPages.init(titles: ["第一个","第二个","第三个"], viewControllers: [MsgSubRXTestCtr(),MsgSubRXTestCtr(),MsgSubRXTestCtr()], parentView: self.view, parentHeaderView: self.view)
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("touches")
    }
}
class testCtr:UIViewController{
    override func viewDidLoad() {
        super.viewDidLoad()
        let arr:[UIColor] = [.red,.blue,.purple,.green,.brown,.darkGray,.orange,.systemPink,.cyan,.gray,.black]
        let backColor:UIColor = arr[ Int(arc4random() % 11)]
        view.backgroundColor = backColor
    }
}
